import Vue from "vue";
import VueRouter from "vue-router";
import Register from "@/views/Register.vue";
import Login from "@/views/Login.vue";
import NotFound from "@/views/404.vue";
import Index from "@/views/Index.vue";
import Home from "@/views/Home.vue";
import Personal from "@/views/Personal.vue";
import FundList from "@/views/FundList.vue";


Vue.use(VueRouter);

const routes = [{
        path: "/",
        component: Index,
        children: [{
                path: '',
                component: Home
            },
            {
                path: '/home',
                name: 'home',
                component: Home
            },
            {
                path: '/personal',
                name: 'personal',
                component: Personal
            },
            {
                path: '/fundlist',
                name: 'fundlist',
                component: FundList
            }
        ]
    },
    {
        path: "/register",
        name: "register",
        component: Register,
    },
    {
        path: "/login",
        name: "login",
        component: Login
    },
    {
        path: "*",
        name: "/404",
        component: NotFound,
    },
];

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes,
});

//路由守卫
router.beforeEach((to, from, next) => {
    //判断是否拥有token

    const isLogin = localStorage.FundToken ? true : false;
    if (to.path == '/login' || to.path == '/register') {
        next();
    } else {
        isLogin ? next() : next('/login');
    }

})

export default router;